package com.mentalhealthplatform.schoolV1.dto;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.mentalhealthplatform.appModels.Teacher;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.util.Date;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;

@Getter
@Setter
@NoArgsConstructor
public class TeacherDTO extends UserDTO {
    private String department;
    private String college;
    private String address;
    @JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", timezone = "UTC")
    private Date birthDate;
    private String name;
    private String number;
    private String gender;

    // 全参构造方法
    public TeacherDTO(UUID id, String username, String phone, boolean status, String department, String college, String address,
                      Date birthDate, String name, String number, String gender, Set<RoleSimpleDTO> roles) {
        super(id, username, phone, status, roles);
        this.department = department;
        this.college = college;
        this.address = address;
        this.birthDate = birthDate;
        this.name = name;
        this.number = number;
        this.gender = gender;
    }

    // 将 Teacher 实体对象转换为 TeacherDTO
    public static TeacherDTO convertToDTO(Teacher teacher) {
        Set<RoleSimpleDTO> roleDTOs = teacher.getRoles().stream()
                .map(role -> new RoleSimpleDTO(role.getId(), role.getIdentifier()))
                .collect(Collectors.toSet());


        return new TeacherDTO(
                teacher.getId(),
                teacher.getUsername(),
                teacher.getPhone(),
                teacher.isStatus(),
                teacher.getDepartment(),
                teacher.getCollege(),
                teacher.getAddress(),
                teacher.getBirthDate(),
                teacher.getName(),
                teacher.getNumber(),
                teacher.getGender(),
                roleDTOs
        );
    }

    // 将 TeacherDTO 转换为 Teacher 实体对象
    public Teacher convertToEntity() {
        Teacher teacher = new Teacher();
        teacher.setId(this.getId());
        teacher.setUsername(this.getUsername());
        teacher.setPhone(this.getPhone());
        teacher.setStatus(this.isStatus());
        teacher.setDepartment(this.department);
        teacher.setCollege(this.college);
        teacher.setAddress(this.address);
        teacher.setBirthDate(this.birthDate);
        teacher.setName(this.name);
        teacher.setNumber(this.number);
        teacher.setGender(this.gender);
        return teacher;
    }
}
